package com.hzjy.api.task;

import com.hzjy.api.beans.HzjyAgreement;
import com.hzjy.api.beans.HzjyDelist;
import com.hzjy.api.beans.HzjyProduct;
import com.hzjy.api.service.impl.ApiDelistServiceImpl;
import com.hzjy.api.service.impl.ApiProductServiceImpl;
import com.hzjy.api.service.impl.HzjyAgreementServiceImpl;
import com.hzjy.common.enums.DelistingStatusEnum;
import com.hzjy.common.enums.TradingStatusEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 每小时检查挂牌、摘牌签订合同超时处理
 */
@Component
public class SignTimeOutTask {
    private static final Logger log = LoggerFactory.getLogger(SignTimeOutTask.class);

    @Autowired
    private ApiProductServiceImpl apiProductServiceImpl;
    @Autowired
    private HzjyAgreementServiceImpl hzjyAgreementServiceImpl;
    @Autowired
    private ApiDelistServiceImpl apiDelistServiceImpl;

    /**
     * 每小时挂牌、摘牌签订合同超时处理
     */
    @Transactional
    public void startSignTimeOutTask() {
        log.info("===========================【每小时挂牌、摘牌签订合同超时处理任务】====================================");
        log.info("==========查询挂牌签约超时的作品=====");
        List<HzjyProduct> productTimeOutList = apiProductServiceImpl.querySignTimeOutProduct();
        if (!CollectionUtils.isEmpty(productTimeOutList)) {
            for (HzjyProduct hzjyProduct : productTimeOutList) {
                //检查之前的协议是否已经生成
                HzjyAgreement oldAgreement = new HzjyAgreement();
                oldAgreement.setUserId(hzjyProduct.getUserId().toString());
                oldAgreement.setProductId(hzjyProduct.getProductId().toString());
                oldAgreement.setType(1);
                List<HzjyAgreement> oldAgreementList = hzjyAgreementServiceImpl.selectHzjyAgreementList(oldAgreement);
                if (!CollectionUtils.isEmpty(oldAgreementList)) {
                    //删除未签署合同
                    for (HzjyAgreement hzjyAgreement : oldAgreementList) {
                        hzjyAgreementServiceImpl.deleteHzjyAgreementById(hzjyAgreement.getId());
                    }
                }
                hzjyProduct.setState(TradingStatusEnum.SignTimeOut.getCode());
                apiProductServiceImpl.updateHzjyProduct(hzjyProduct);
            }
        }
        log.info("==========查询摘牌签约超时的作品=====");
        List<HzjyDelist> hzjyDelistList = apiDelistServiceImpl.querySignTimeOutDelist();
        if (!CollectionUtils.isEmpty(hzjyDelistList)) {
            for (HzjyDelist hzjyDelist : hzjyDelistList) {
                //检查之前的协议是否已经生成
                HzjyAgreement oldAgreement = new HzjyAgreement();
                oldAgreement.setUserId(hzjyDelist.getUserId());
                oldAgreement.setProductId(hzjyDelist.getProductId().toString());
                oldAgreement.setDelistId(hzjyDelist.getDelistId());
                oldAgreement.setType(2);
                List<HzjyAgreement> oldAgreementList = hzjyAgreementServiceImpl.selectHzjyAgreementList(oldAgreement);
                if (!CollectionUtils.isEmpty(oldAgreementList)) {
                    //删除未签署合同
                    for (HzjyAgreement hzjyAgreement : oldAgreementList) {
                        hzjyAgreementServiceImpl.deleteHzjyAgreementById(hzjyAgreement.getId());
                    }
                }
                if (hzjyDelist.getStatus() == 54){
                    //摘牌双方协议签约超时
                    hzjyDelist.setStatus(DelistingStatusEnum.SignTimeOutTwo.getCode());
                }else {
                    //摘牌平台协议签约超时
                    hzjyDelist.setStatus(DelistingStatusEnum.SignTimeOut.getCode());
                }
                apiDelistServiceImpl.updateHzjyDelist(hzjyDelist);
            }
        }

        log.info("===========================【每小时挂牌、摘牌签订合同超时处理任务结束】====================================");
    }
}
